package com.beiding.demo;

import com.beiding.demo.bean.DispatcherImpl;
import com.beiding.demo.bean.ExecutorImpl;
import com.beiding.mpTimer.*;
import com.beiding.mpTimer.impl.TimerShaftServiceInMemory;

import java.io.IOException;

public class Main {


    public static void main(String[] args) throws IOException {


        //创建一个时间轴服务
        TimerShaftService service = new TimerShaftServiceInMemory();

        //创建一个执行者
        Executor executor = new ExecutorImpl(service);

        //添加一个处理器
        executor.addHandler(new Handler<String>("test") {
            @Override
            protected void handle(String payload, long time) {

                if (payload.equals("将会抛出异常")) {
                    throw new RuntimeException();
                }

                System.out.println("负载为:" + payload);
                System.out.println("发生时间为:" + time);

            }
        });

        //创建一个任务派发器
        Dispatcher dispatcher = new DispatcherImpl(service, executor);

        //创建时间轴对象
        TimerShaft timerShaft = new TimerShaftImpl(service, 1000, 5000, dispatcher, new IdCreateStrategy() {
            @Override
            public String createId(Object payload, String topic) {

                return System.currentTimeMillis() + payload.hashCode() + "";
            }
        });


        //延时三个任务

        timerShaft.delay("无处理器", "abc", 5000);

        timerShaft.delay("正常执行", "test", 2000);

        timerShaft.delay("将会抛出异常", "test", 4000);

    }


}
